package omp.syst.jui;

import omp.syst.eng.*;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.*;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SystemJUI extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static String options[] = { "Invert", "Log Transform", "Power Law Transform", "Histogram Equalization" };
	
	private JButton newButton, applyButton;
	private JLabel descLabel;
	private JComboBox<String> optionsComboBox;
	private DisplayPanel centerPanel;
	private SystemEngine engine;
	
	public SystemJUI(){
		
		super( "Gesture Recogniton System" );
		
		JPanel northPanel = new JPanel();
		
		descLabel = new JLabel( "No image loaded" );
		northPanel.add( descLabel );
		
		add( northPanel, BorderLayout.NORTH );
		
		centerPanel = new DisplayPanel();
		add( centerPanel, BorderLayout.CENTER );
		
		JPanel southPanel = new JPanel();
		
		newButton = new JButton( "New" );
		newButton.addActionListener(
				
				new ActionListener(){
					
					public void actionPerformed( ActionEvent e ){
						
						getImageFromDirectory();
					}
				}
		);
		southPanel.add( newButton );
		
		optionsComboBox = new JComboBox<String>( options );
		southPanel.add( optionsComboBox );
		
		applyButton = new JButton( "Apply" );
		applyButton.addActionListener(
				
				new ActionListener(){
					
					public void actionPerformed( ActionEvent e ){
						applyChange( optionsComboBox.getSelectedIndex() );
					}
				}
		);
		southPanel.add( applyButton );
		
		add( southPanel, BorderLayout.SOUTH );
		
		setSize( 500, 500 );
		setVisible( true );
	}
	
	public void getImageFromDirectory(){
		
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY );
		fileChooser.showOpenDialog( this );
		
		File f = fileChooser.getSelectedFile();
		
		if( !f.isFile() )
			return;
		else{
			
			
			try{
				
				engine = new SystemEngine();
				centerPanel.loadNewImage( engine.ignite( ImageIO.read( new File( f.getAbsolutePath() ) ) ) );

			}
			catch( IOException e ){
				
			}
		}
		
	}
	
	private void applyChange( int opt ){
		
		switch( opt ){
		
		case 4:
			centerPanel.loadNewImage( engine.enhanceImage( Enhancer.TYPE_HST_TRANSFORM ) );
		}
	}
	
	public static void main( String args[] ){
		
		SystemJUI app = new SystemJUI();
		app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
}
